from random import randint
from lib.ZButton import ZButton
from lib.res.fun import _create_image , _create_sound
from lib.res.glo import SRC
from resource.script.Enemy.lib.Enemy import Enemy
from resource.script.Eve.lib.Eve import Eve
from resource.script.Item.lib.Item import Item
from resource.script.Map.res.glo import _m_src


class Block ( ZButton ) :
	def __init__ ( self , father , x , y , width , height , type = 0 , status = 0 ) :
		self.close = _create_image ( _m_src [ 'image' ] [ 'block' ] [ 'close' ] )
		self.shadow = _create_image ( _m_src [ 'image' ] [ 'block' ] [ 'shadow' ] )
		self.open = _create_image ( _m_src [ 'image' ] [ 'block' ] [ 'open' ] )
		super ( Block , self ).__init__ ( father , self.close , self.close )
		self.width , self.height = width , height
		self.x , self.y = x , y
		self._pos = self.x * self.width , self.y * self.height
		self.status = status  # 0,1,2
		self.type = type  # 0,1,2,3,4...... 0 - none ; 1 - item ; 2 - enemy ; 3 -
		self.isenemy = randint ( 0 , 1 )
		if self.type :
			if self.type == 1 :
				self.item = Item ( self , randint ( 0 , 2 ) )
				self.item._running = False
			elif self.type == 2 :
				level = 1
				self.enemy = Enemy ( self , randint ( 0 , 1 ) , level )
				self.enemy._running = False
			elif self.type == 3 :
				self.eve = Eve ( self , randint ( 0 , 2 ) )
				self.eve._running = False
		self.clicksound = _create_sound ( SRC [ 'sound' ] [ 'block' ] )

	def draw ( self ) :
		if not self.status :
			self.father.screen.blit ( self.close , self._pos )
		elif self.status == 1 :
			self.father.screen.blit ( self.shadow , self._pos )
		else :
			self.father.screen.blit ( self.open , self._pos )
			if self.type == 1 :
				self.item._running = True
				self.item.draw ( )
			elif self.type == 2 :
				self.enemy._running = True
				self.enemy.draw ( )
			elif self.type == 3 :
				self.eve._running = True
				self.eve.draw ( )

	def _action ( self ) :
		if (self.status == 2 and self.type == 2) or (self.status == 1) :
			self._enemy_attack ( )
		if self.status == 1 :
			self.clicksound.play ( )
			self.__change ( self.x , self.y )
			from resource.ui.sces import players
			players [ 0 ]._hit_js ( 1 )

	def update ( self , event , mouse_pos , mouse_key ) :
		super ( Block , self ).update ( event , mouse_pos , mouse_key )
		if self.type :
			if self.type == 1 :
				self.item.update ( event , mouse_pos , mouse_key )
			elif self.type == 2 :
				self.enemy.update ( event , mouse_pos , mouse_key )

	def __change ( self , x , y ) :
		lists = [ ]
		top = x , y - 1
		bottom = x , y + 1
		left = x - 1 , y
		right = x + 1 , y
		lists.append ( top )
		lists.append ( bottom )
		lists.append ( left )
		lists.append ( right )
		for list in lists :
			for x in list :
				if x == -1 or x == len ( self.father.blocks ) :
					lists.remove ( list )

		for x in lists :
			for y in x :
				if y < 0 or x == len ( self.father.blocks ) :
					lists.remove ( x )

		for block in lists :
			this = self.father.blocks [ block [ 0 ] ] [ block [ 1 ] ]
			if this.status == 2 :
				lists.remove ( block )
		for x in lists :
			this = self.father.blocks [ x [ 0 ] ] [ x [ 1 ] ]
			if not this.status :
				this.status = 1
			self.status = 2

	def _enemy_attack ( self ) :
		for x in self.father.blocks :
			for y in self.father.blocks [ x ] :
				if self.father.blocks [ x ] [ y ].status == 2 and self.father.blocks [ x ] [ y ].type == 2 :
					self.father.blocks [ x ] [ y ].enemy.attack ( )
